package wf3.filter;

import acceso.util.Escritor;
import acceso.util.LogueoHelper;
import acceso.util.UserSesion;
import acceso.util.WorkflowUtil;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import wf3.core.Configuration;

/**
 *
 * @author Rómulo Galindo Tanta
 */
public class LoginFilter implements Filter {

    FilterConfig filterConfig = null;
    static String cookie_name = "";

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        Escritor.escribe_debug("[filtro: LoginFilter] [path: " + req.getRequestURI() + (req.getQueryString() != null ? "?" + req.getQueryString() : "") + "]");
        Cookie cookie = getCookie(req);
        HttpSession ses = req.getSession();
        UserSesion US = ses.getAttribute("US") != null ? (UserSesion) ses.getAttribute("US") : null;
        Configuration CFG = (Configuration) request.getServletContext().getAttribute("CFG");

        if (cookie != null & US != null) {
            if (WorkflowUtil.convertir_entero(cookie.getValue()) == US.getId_sesion()) {
                res.sendRedirect(US.getNo_modini() == null | US.getNo_modini().contentEquals("") ? CFG.getWf_modini() : US.getNo_modini().replaceAll("../", ""));
                return;
            }

            ses.removeAttribute("US");
            ses.invalidate();
            chain.doFilter(request, response);
            return;
        }

        if (cookie != null & US == null) {
            if (LogueoHelper.loguear(req, WorkflowUtil.convertir_entero(cookie.getValue()))) {
                US = (UserSesion) ses.getAttribute("US");
                res.sendRedirect(US.getNo_modini() == null | US.getNo_modini().contentEquals("") ? CFG.getWf_modini() : US.getNo_modini().replaceAll("../", ""));
                return;
            }
        }

        ses.invalidate();
        chain.doFilter(request, response);
    }

    public FilterConfig getFilterConfig() {
        return (this.filterConfig);
    }

    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig filterConfig) {
        Escritor.escribe_debug("Iniciando Filtro(LoginFilter)");
        this.filterConfig = filterConfig;
        cookie_name = ((Configuration) filterConfig.getServletContext().getAttribute("CFG")).getCookie_name();
        Escritor.escribe_debug("Filtro(LoginFilter) con cookie_name=" + cookie_name);
        Escritor.escribe_info("Filtro (LoginFilter) Iniciado");
    }

    public synchronized static void update_filter(boolean l) {
    }

    public Cookie getCookie(HttpServletRequest req) {
        Cookie cookie = null;
        Cookie[] cookies = req.getCookies();
        if (cookies != null && cookies.length > 0) {
            try {
                for (Cookie ck : cookies) {
                    if (ck.getName().contentEquals(cookie_name) && !ck.getValue().isEmpty()) {
                        cookie = ck;
                        break;
                    }
                }
            } catch (Exception ep) {
                System.out.println(">>" + ep.getMessage());
            }
        }
        return cookie;
    }

}
